package com.usaas.search.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usaas.common.core.domain.AjaxResult;
import com.usaas.search.dto.PlatWelfareMessageDto;
import com.usaas.search.mapper.PlatWelfareMessageMapper;
import com.usaas.search.pojo.PlatWelfareMessagePojo;
import com.usaas.search.service.IPlatWelfareMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 福利通知Service业务层处理
 *
 * @author Helios
 * @date 2024-08-14
 */
@Slf4j
@Service
public class PlatWelfareMessageServiceImpl extends ServiceImpl<PlatWelfareMessageMapper, PlatWelfareMessagePojo> implements IPlatWelfareMessageService {

    @Resource
    private PlatWelfareMessageMapper platWelfareMessageMapper;

    /**
     * 查询福利通知
     *
     * @param id 福利通知主键
     * @return 福利通知
     */
    @Override
    public PlatWelfareMessageDto queryById(Long id) {
        PlatWelfareMessagePojo pojo = this.getById(id);
        if (pojo != null) {
            PlatWelfareMessageDto dto = BeanUtil.copyProperties(pojo, PlatWelfareMessageDto.class);
            List<PlatWelfareMessageDto> dtos = ListUtil.of(dto);
            convertList(dtos);
            return dtos.get(0);
        }
        return null;
    }

    /**
     * 查询福利通知列表
     *
     * @param platWelfareMessageDto 福利通知
     * @return 福利通知
     */
    @Override
    public List<PlatWelfareMessageDto> queryList(PlatWelfareMessageDto platWelfareMessageDto) {
        LambdaQueryWrapper<PlatWelfareMessagePojo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = this.buildWrapper(queryWrapper, platWelfareMessageDto);
        if (queryWrapper == null) {
            return null;
        }
        List<PlatWelfareMessagePojo> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }

        List<PlatWelfareMessageDto> dtoList = BeanUtil.copyToList(list, PlatWelfareMessageDto.class);
        convertList(dtoList);
        return dtoList;
    }

    private void convertList(List<PlatWelfareMessageDto> dtoList) {
        if (CollectionUtil.isEmpty(dtoList)) {
            return;
        }
    }

    /**
     * 新增福利通知
     *
     * @param platWelfareMessageDto 福利通知
     * @return 结果
     */
    @Override
    @Transactional
    public boolean add(PlatWelfareMessageDto platWelfareMessageDto) {

        PlatWelfareMessagePojo platWelfareMessagePojo = BeanUtil.copyProperties(platWelfareMessageDto, PlatWelfareMessagePojo.class);
        return this.save(platWelfareMessagePojo);
    }

    /**
     * 修改福利通知
     *
     * @param platWelfareMessageDto 福利通知
     * @return 结果
     */
    @Override
    @Transactional
    public boolean update(PlatWelfareMessageDto platWelfareMessageDto) {
        PlatWelfareMessagePojo platWelfareMessagePojo = BeanUtil.copyProperties(platWelfareMessageDto, PlatWelfareMessagePojo.class);
        return this.updateById(platWelfareMessagePojo);
    }

    /**
     * 批量删除福利通知
     *
     * @param ids 需要删除的福利通知主键
     * @return 结果
     */
    @Override
    @Transactional
    public boolean removeBatch(List<String> ids) {
        List<Long> idsLong = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return this.removeBatchByIds(idsLong);
    }


    /**
     * 分页查询${subTable.functionName}信息
     *
     * @param platWelfareMessageDto
     * @return
     */
    @Override
    public AjaxResult queryPage(PlatWelfareMessageDto platWelfareMessageDto) {
        Page<PlatWelfareMessagePojo> page = new Page<>(platWelfareMessageDto.getPageNum(), platWelfareMessageDto.getPageSize());
        LambdaQueryWrapper<PlatWelfareMessagePojo> wrapper = new LambdaQueryWrapper<PlatWelfareMessagePojo>();
        //此处可做前置判断
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        wrapper = buildWrapper(wrapper, platWelfareMessageDto);
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        Page<PlatWelfareMessagePojo> pageResult = this.page(page, wrapper);
        Page<PlatWelfareMessageDto> finalResult = BeanUtil.copyProperties(pageResult, Page.class);
        List<PlatWelfareMessagePojo> records = pageResult.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<PlatWelfareMessageDto> dtos = BeanUtil.copyToList(records, PlatWelfareMessageDto.class);
            convertList(dtos);
            finalResult.setRecords(dtos);
        }
        return AjaxResult.success(finalResult);
    }

    private LambdaQueryWrapper<PlatWelfareMessagePojo> buildWrapper(LambdaQueryWrapper<PlatWelfareMessagePojo> wrapper, PlatWelfareMessageDto param) {
        log.info("buildPageWrapper param :{}", param);
        if (wrapper == null) {
            return null;
        }
        Long id = param.getId();
        if (id != null) {
            wrapper.eq(PlatWelfareMessagePojo::getId, id);
        }
        String mainTitle = param.getMainTitle();
        if (StrUtil.isNotBlank(mainTitle)) {
            wrapper.eq(PlatWelfareMessagePojo::getMainTitle, mainTitle);
        }
        String subTitle = param.getSubTitle();
        if (StrUtil.isNotBlank(subTitle)) {
            wrapper.eq(PlatWelfareMessagePojo::getSubTitle, subTitle);
        }
        String name = param.getName();
        if (StrUtil.isNotBlank(name)) {
            wrapper.eq(PlatWelfareMessagePojo::getName, name);
        }
        String icons = param.getIcons();
        if (StrUtil.isNotBlank(icons)) {
            wrapper.eq(PlatWelfareMessagePojo::getIcons, icons);
        }
        Long gameInfoId = param.getGameInfoId();
        if (gameInfoId != null) {
            wrapper.eq(PlatWelfareMessagePojo::getGameInfoId, gameInfoId);
        }
        String versionDate = param.getVersionDate();
        if (StrUtil.isNotBlank(versionDate)) {
            wrapper.eq(PlatWelfareMessagePojo::getVersionDate, versionDate);
        }
        String messageDesc = param.getMessageDesc();
        if (StrUtil.isNotBlank(messageDesc)) {
            wrapper.eq(PlatWelfareMessagePojo::getMessageDesc, messageDesc);
        }
        String contentDesc = param.getContentDesc();
        if (StrUtil.isNotBlank(contentDesc)) {
            wrapper.eq(PlatWelfareMessagePojo::getContentDesc, contentDesc);
        }
        Long stars = param.getStars();
        if (stars != null) {
            wrapper.eq(PlatWelfareMessagePojo::getStars, stars);
        }
        Long hotLevel = param.getHotLevel();
        if (hotLevel != null) {
            wrapper.eq(PlatWelfareMessagePojo::getHotLevel, hotLevel);
        }
        Long collectNums = param.getCollectNums();
        if (collectNums != null) {
            wrapper.eq(PlatWelfareMessagePojo::getCollectNums, collectNums);
        }
        Long clickNum = param.getClickNum();
        if (clickNum != null) {
            wrapper.eq(PlatWelfareMessagePojo::getClickNum, clickNum);
        }
        String createBy = param.getCreateBy();
        if (StrUtil.isNotBlank(createBy)) {
            wrapper.eq(PlatWelfareMessagePojo::getCreateBy, createBy);
        }
        Date createTime = param.getCreateTime();
        if (createTime != null) {
            wrapper.eq(PlatWelfareMessagePojo::getCreateTime, createTime);
        }
        String updateBy = param.getUpdateBy();
        if (StrUtil.isNotBlank(updateBy)) {
            wrapper.eq(PlatWelfareMessagePojo::getUpdateBy, updateBy);
        }
        Date updateTime = param.getUpdateTime();
        if (updateTime != null) {
            wrapper.eq(PlatWelfareMessagePojo::getUpdateTime, updateTime);
        }
        String remark = param.getRemark();
        if (StrUtil.isNotBlank(remark)) {
            wrapper.eq(PlatWelfareMessagePojo::getRemark, remark);
        }
        Long delFlag = param.getDelFlag();
        if (delFlag != null) {
            wrapper.eq(PlatWelfareMessagePojo::getDelFlag, delFlag);
        }
        log.info("buildPageWrapper result :{}", wrapper);
        return wrapper;
    }
}
